package com.lgx.mall.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.entity.CityDictonary;
import com.lgx.common.entity.IntraCity;
import com.lgx.common.service.ICityDictonaryService;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.core.service.IAdvertisementService;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.vo.ProductVO;
import com.lgx.mall.view.service.IWebPageDetailService;

/**
 * 共享同城
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/city")
public class CityController {

	
	@Autowired
	private IWebPageDetailService webPageDetailService;
	@Autowired
	private IProductService productService;
	@Autowired
	private ICityDictonaryService cityDictonaryService;
	@Autowired
	private IAdvertisementService advertisementService;
	
	public IAdvertisementService getAdvertisementService() {
		return advertisementService;
	}

	public void setAdvertisementService(IAdvertisementService advertisementService) {
		this.advertisementService = advertisementService;
	}

	public ICityDictonaryService getCityDictonaryService() {
		return cityDictonaryService;
	}

	public void setCityDictonaryService(ICityDictonaryService cityDictonaryService) {
		this.cityDictonaryService = cityDictonaryService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public IWebPageDetailService getWebPageDetailService() {
		return webPageDetailService;
	}

	public void setWebPageDetailService(IWebPageDetailService webPageDetailService) {
		this.webPageDetailService = webPageDetailService;
	}

	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		String pageNo1 = request.getParameter("pageNo1");
		if(StringUtils.isEmpty(pageNo1))pageNo1 = "1";
		request.setAttribute("pageNo1", pageNo1);
		
		String pageNo2 = request.getParameter("pageNo2");
		if(StringUtils.isEmpty(pageNo2))pageNo2 = "1"; 
		request.setAttribute("pageNo2", pageNo2);
		
		String pageNo3 = request.getParameter("pageNo3");
		if(StringUtils.isEmpty(pageNo3))pageNo3 = "1";
		request.setAttribute("pageNo3", pageNo3);
		
		String county = request.getParameter("county");
		if(StringUtils.isEmpty(county))county = "0";
		request.setAttribute("county", county);
		
		String scoreType = request.getParameter("scoreType");
		if(StringUtils.isEmpty(scoreType))scoreType = "0";
		request.setAttribute("scoreType", scoreType);
		
		String sortNumber = request.getParameter("sortNumber");
		if(StringUtils.isEmpty(sortNumber))sortNumber = "0";
		request.setAttribute("sortNumber", sortNumber);
		
		return "city/index";
	}
	
	@RequestMapping("/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "city/head";
	}
	
	/**
	 * 热门同城
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/grid2m.ftl")
	public String grid2m(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//获取同城
		IntraCity areaNode = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		long categoryId = 126;
		List<ProductVO> details1 = this.webPageDetailService.findProductVOByAreaAndCid(areaNode.getAreaId(), categoryId);
		categoryId = 153;
		List<ProductVO> details2 = this.webPageDetailService.findProductVOByAreaAndCid(areaNode.getAreaId(), categoryId);
		
		request.setAttribute("details1", details1);
		request.setAttribute("details2", details2);
		
		return "city/grid2m";
	}
	
	/**
	 * 同城兑换
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/city-product.ftl")
	public String cityProduct(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		long categoryId = 5;
		String scoreType = request.getParameter("scoreType");	
		String sortNumber = request.getParameter("sortNumber");	
		String pageNo = request.getParameter("pageNo1");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		
		//获取同城
		IntraCity areaNode = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		Pagination page = this.productService.findProductByCategoryAndPrice2(areaNode.getAreaId(),categoryId, scoreType,sortNumber, 18, Integer.parseInt(pageNo));
		
		request.setAttribute("page", page);
		request.setAttribute("pageNo1", pageNo);
		request.setAttribute("scoreType", scoreType);
		request.setAttribute("sortNumber", sortNumber);
		
		return "city/city-product";
	}
	/**
	 * 通栏广告
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ad-1000.ftl")
	public String ad1000(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//获取同城
		IntraCity areaNode = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		long categoryId = 128;
		List<ProductVO> details = this.webPageDetailService.findProductVOByAreaAndCid(areaNode.getAreaId(), categoryId);
		request.setAttribute("details", details);
		
		return "city/ad-1000";
	}
	
	/**
	 * 同城商家
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/business-pro.ftl")
	public String businessPro(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//获取同城
		IntraCity areaNode = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		//获取县区
		List<CityDictonary> citys = this.cityDictonaryService.findCityDictonaryList("county", areaNode.getAreaId()+"");
		
		
		String county = request.getParameter("county");
		if(!StringUtils.isEmpty(county)) county = EscapeUtils.unescape(county);
		if("0".equals(county)) county = "";
		
		String pageNo = request.getParameter("pageNo2");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		
		Pagination page = this.advertisementService.findAdvertisementPagination(areaNode.getAreaId(), county, 9,Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		request.setAttribute("pageNo2", pageNo);
		if(StringUtils.isEmpty(county)) county = "0";
		request.setAttribute("county", county);
		request.setAttribute("citys", citys);
		
		return "city/business-pro";
	}
	
	/**
	 * 同城推广
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/city-promotion.ftl")
	public String cityPromotion(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//获取同城
		IntraCity areaNode = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		long categoryId = 130;
		String pageNo = request.getParameter("pageNo3");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		
		Pagination page = this.webPageDetailService.findWebPageDetailPaginationByAreaAndCid(areaNode.getAreaId(), categoryId, 15, Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		request.setAttribute("pageNo3", pageNo);
		
		return "city/city-promotion";
	}
}
